package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.hosp.bean.Result;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.utils.HttpRequestHelper;
import com.atguigu.yygh.model.hosp.Hospital;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2025/3/15 22:09
 */
@RestController
@RequestMapping("/api/hosp")
public class ApiHospitalController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    // 保存医院信息到mongoDB中
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        //前端传过来的数据都是一个key对应一个value的map
        Map<String, Object> map = HttpRequestHelper.switchRequest(request);
        //判断请求是否合法
        String hoscode = (String) map.get("hoscode");
        if (StringUtils.isEmpty(hoscode)){
            return Result.fail().message("hoscode不能为空");
        }
        //判断签名是否正确(加密后的)
        String sign = (String) map.get("sign");
        //去hospitalSet库去查询加密后的签名
        String trueSign = hospitalSetService.getSign(hoscode);
        //判断签名是否正确
        if (!StringUtils.isEmpty(sign)&&!StringUtils.isEmpty(trueSign)&&trueSign.equals(sign)){
            //签名正确，可以保存数据
            hospitalService.saveHospital(map);
            return Result.ok().message("保存成功");
        }


        //先判断
        return Result.fail().message("签名错误");
    }
    //根据hoscode去mongoDB查询医院信息
    @PostMapping("/hospital/show")
    public Result<Hospital>  showHospital(HttpServletRequest request) {
        Map<String, Object> map = HttpRequestHelper.switchRequest(request);
        Hospital hospital=hospitalService.findHospitalByHoscode(map);
        return Result.ok(hospital);
    }
}
